package com.example.demo.service;

import com.example.demo.config.TestConfig;
import com.example.demo.dao.UserTestDao;
import com.example.demo.service.impl.UserTestServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.test.util.ReflectionTestUtils;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class ServiceTest2 {

    //@Mock(answer = Answers.RETURNS_DEEP_STUBS)
    //private UserTestDao testDao = Mockito.mock(UserTestDao.class);

    @Configuration
    static class TestConfiguration{

//        @Bean
//        @Primary
//        public UserTestDao testDao(){
//            return new UserTestDao();
//        }
//
//        @Bean
//        @Primary
//        public UserTestServiceImpl userTestServiceImpl(){
//            return new UserTestServiceImpl();
//        }
    }

    //@Autowired
    @Mock
    private UserTestDao testDao;

    @Mock
    private TestConfig testConfig;

    @InjectMocks
    private UserTestServiceImpl userTestServiceImpl;

    @Test
    public void getGetAccountBalance(){
        Mockito.when(testConfig.rest()).thenReturn("mock Rest");
        ReflectionTestUtils.setField(testDao,"testConfig",testConfig);
        //UserTestDao testDao = Mockito.mock(UserTestDao.class,Mockito.RETURNS_DEEP_STUBS);
        Mockito.when(testDao.getWmnAcount()).thenReturn("mock WmnAccount");
        Mockito.doCallRealMethod().when(testDao).getAccountBalance();

        String accountBalance = userTestServiceImpl.getAccountBalance();

        System.out.println("==========>"+accountBalance);
    }
}
